package com.yc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.SceneCreateParam;
import com.yc.cloud.admin.dto.request.SceneUpdateParam;
import com.yc.cloud.admin.service.SceneService;
import com.yc.cloud.admin.vo.SceneVo;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.Scene;
import com.yc.cloud.mapper.SceneMapper;
import com.yc.cloud.oss.service.OssFileService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-03-28
 */
@Service
public class SceneServiceImpl extends ServiceImpl<SceneMapper, Scene> implements SceneService {

    private final OssFileService ossFileService;

    public SceneServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<SceneVo> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<Scene>();
        IPage<Scene> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val result = baseMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public Scene create(SceneCreateParam param) {
        val scene = new Scene();
        BeanUtils.copyProperties(param, scene);
//        if (param.isOpen()) {
//            scene.setCompanyId();
//        }
        val saveFlag = save(scene);
        return saveFlag ? scene : null;
    }

    @Override
    public Scene get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public Scene update(Long id, SceneUpdateParam param) {
        var scene = getById(id);
        if (scene == null) {
            return null;
        }
        BeanUtils.copyProperties(param, scene);
        val updateFlag = updateById(scene);
        return updateFlag ? scene : null;
    }


    @Override
    public SceneVo convertVo(Scene scene) {
        if (scene == null) {
            return null;
        }
        val vo = new SceneVo();
        BeanUtils.copyProperties(scene, vo);
        val url = ossFileService.getOssFileUrl(String.valueOf(vo.getFileId()));
        val coverUrl = ossFileService.getOssFileUrl(String.valueOf(vo.getCover()));
        val lightUrl = ossFileService.getOssFileUrl(String.valueOf(vo.getLight()));
        vo.setUrl(url);
        vo.setCoverUrl(coverUrl);
        vo.setLightUrl(lightUrl);
        return vo;
    }

    @Override
    public List<SceneVo> convertVo(List<Scene> SceneFileList) {
        if (CollUtil.isNotEmpty(SceneFileList)) {
            return SceneFileList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
